function loadRoleList(){
	$("#role_grid").jqGridInit({
		url : "/admin/role/list.do",
		colNames : [ 'ID', '角色名称','具体描述' ],
        colModel : [ 
                     {name : 'role_id',index : 'role_id',hidden:true,key:true,}, 
                     {name : 'role_name',index : 'role_name',editable: true,editrules:{required:true,}}, 
                     {name : 'role_desc',index : 'role_desc',editable: true,editrules:{required:true,}}, 
                   ],
        editurl: "/admin/role/edit.do",//编辑保存后台的值
       	autowidth:true,
        height:400,
        onSelectRow:function(id){
        	var param = {};
        	param.role_id = id;
        	loadRolePrivilegeTree(param);
        	$("#operation_btn_div").hide();
        	var rowData = $("#role_grid").jqGrid("getRowData",id);
        	$("#rolePrivilegeTitle").html(rowData.role_name+"-角色权限");
        	$("#rolePrivilegeTitle").attr("role_id",id);
	    },
	});
	//调用系统封装的底部工具
	$("#role_grid").navGridInit({
		add: {},
		del:{},
		edit:{},
		search: {},
		refresh: {},
		view: {}
	});
}


var setting = {
	check: {
		enable: true,
		chkboxType:{ "Y" : "s", "N" : "s" }
	},
	data: {
		simpleData: {
			enable: true,
			idKey: "id",
            pIdKey: "pid",
            rootPId: ""
		},
		key:{name:"name"}
	},
	callback:{
		onCheck:function(event, treeId, treeNode) {
			$("#operation_btn_div").show();
//		    alert(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked);
		},
		//单击事件
//		onClick:treeOnClick
	}
}

function loadRolePrivilegeTree(param){
	$.ajax({
		url : "/admin/role/loadRolePrivilegeTree.do",
		data:param,
		type:"post",
		dataType:"json",
		success : function(data) {
			rolePrivilegeTreeData = data;
			$.fn.zTree.init($("#rolePrivilegeTree"), setting, data);
			$("#rolePrivilegeTree_div").show();
		}
	});
}
$(function(){
	loadRoleList();
	//loadRolePrivilegeTree();
})
var rolePrivilegeTreeData;
function btnSubmit(){
	if(!rolePrivilegeTreeData){
		$.gritter.add({
			title: '提示',
			text: "请先选择一个角色进行操作",
			class_name: 'gritter-success'
		});
		return ;
	}
	var param = {};
	var role_id = $("#rolePrivilegeTitle").attr("role_id");
	var rolePrivilegeTree =  $.fn.zTree.getZTreeObj("rolePrivilegeTree");
	var add = [];
	var del = [];
	
	for(var i=0;i<rolePrivilegeTreeData.length;i++){
		var op = rolePrivilegeTreeData[i];
		var np = rolePrivilegeTree.getNodeByParam("id", op.id, null);
		var obj = {};
		obj.privilege_obj_id = op.id;
		obj.privilege_type = op.type;
		obj.role_id = role_id;
		
		if(op.checked == "" && np.checked == true){
			add.push(obj);
		}else if(op.checked == "true" && np.checked == false){
			obj.privilege_rel_id = op.privilege_rel_id;
			del.push(obj);
		}
	}
	console.log(param);
	param.add = add;
	param.del = del;
	$.ajax({
		url : "/admin/role/saveRolePrivilege.do",
		data:{json:JSON.stringify(param)},
		type:"post",
		dataType:"json",
		success : function(data) {
			$.gritter.add({
				title: '提示',
				text: data.msg,
				class_name: 'gritter-success'
			});
			btnCancelOnClick();
		}
	});
}
function btnCancelOnClick(){
	var param = {};
	param.role_id = $("#rolePrivilegeTitle").attr("role_id");
	loadRolePrivilegeTree(param);
	$("#operation_btn_div").hide();
}